﻿Public Class TenKey
    Protected btnKeyAry() As System.Windows.Forms.Button
    'Private lblInput As System.Windows.Forms.Label
    Private lblInput As TenKeyLabel

    Public Event InputCompleted(ByVal Sender As Object)

    Public Sub New(ByRef InputLabel As TenKeyLabel)

        ' この呼び出しはデザイナーで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後で初期化を追加します。

        lblInput = InputLabel
    End Sub

    Private Sub TenKey_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Me.Visible = False
        Me.BringToFront()
        btnKeyAry = New System.Windows.Forms.Button(9) {}
        For i = 0 To 9
            btnKeyAry(i) = CType(Me.Panel1.Controls("btn" & i), Button)
        Next
        btnC = CType(Me.Panel1.Controls("btnC"), Button)
        btnE = CType(Me.Panel1.Controls("btnE"), Button)
        For i = 0 To btnKeyAry.Length - 1
            AddHandler btnKeyAry(i).Click, AddressOf btnKeyAry_Click
        Next i
        AddHandler btnC.Click, AddressOf btnC_Click
        AddHandler btnE.Click, AddressOf btnE_Click

        Select Case lblInput.Type
            Case TenKeyLabel.TenkeyType.INT
                btnDot.Enabled = False
                btnDot.Visible = True
                btnBar.Enabled = lblInput.AllowMinus
                btnL.Enabled = False
                btnL.Visible = False
                AddHandler btnBar.Click, AddressOf btnbar_Click
            Case TenKeyLabel.TenkeyType.FLT
                btnDot.Enabled = True
                btnDot.Visible = True
                btnBar.Enabled = lblInput.AllowMinus
                btnL.Enabled = False
                btnL.Visible = False
                AddHandler btnBar.Click, AddressOf btnbar_Click
            Case TenKeyLabel.TenkeyType.TYP
                btnDot.Enabled = False
                btnDot.Visible = False
                btnBar.Enabled = True
                btnL.Enabled = True
                btnL.Visible = True
                AddHandler btnBar.Click, AddressOf btnbar_Click
                AddHandler btnL.Click, AddressOf btnL_Click
        End Select
    End Sub

#Region "セル動作"
    'Protected Sub CallTenKey()
    '    lblInput.BackColor = Color.Cyan
    '    Dim iDifTop As Integer = 0
    '    If lblInput.Parent.Name.ToUpper.StartsWith("TAB") Then
    '        iDifTop = lblInput.Parent.Parent.Top + lblInput.Parent.Top
    '    End If
    '    Me.Top = iDifTop + lblInput.Bottom + 5
    '    Me.Left = lblInput.Right - Me.Width
    '    If Me.Left < 0 Then
    '        Me.Left = 0
    '    End If
    '    If Me.Bottom > Me.Height Then
    '        Me.Top = lblInput.Top - Me.Height - 5 + iDifTop
    '    End If
    '    Me.Visible = True

    '    Dim fIsTenkeyL As Boolean
    '    Try
    '        fIsTenkeyL = lblInput.Tag.Equals("BiEq")
    '    Catch
    '        fIsTenkeyL = False
    '    End Try

    '    'If fIsTenkeyL Then
    '    '    btnBar.Visible = True
    '    '    btnL.Visible = True
    '    '    btnE.Height = 188
    '    '    btnC.Left = 193
    '    'Else
    '    '    btnBar.Visible = False
    '    '    btnL.Visible = False
    '    '    btnE.Height = 252
    '    '    btnC.Left = 129
    '    'End If
    'End Sub

    'Protected Sub TenKeyClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    lblInput = CType(sender, Label)
    '    m_DelayType = DelayType.Tenkey
    '    CallTenKey()
    'End Sub

    'Protected Sub TenKeyMouseEnter(ByVal sender As Object, ByVal e As System.EventArgs)
    '    CType(sender, Label).BackColor = Color.DodgerBlue
    'End Sub

    'Protected Sub TenkeyMouseLeave(ByVal sender As Object, ByVal e As System.EventArgs)
    '    CType(sender, Label).BackColor = Color.Yellow
    '    If Me.Visible Then
    '        lblInput.BackColor = Color.Cyan
    '    End If
    'End Sub

#End Region

#Region "キー入力処理"
    '******************************************************************************
    '*  キー入力処理
    '******************************************************************************
    Protected Sub btnKeyAry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim btnKey As Button = CType(sender, Button)
        Dim strKey As String = btnKey.Text
        If lblInput.Text.Equals("0") Or Not lblInput.IsChanged Then
            lblInput.Text = strKey
        Else
            lblInput.Text &= strKey
        End If
    End Sub

    Private Sub btnDot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDot.Click
        Dim chrArr() As Char = lblInput.text.ToCharArray
        For Each chara As Char In chrArr
            If chara = CChar(".") Then
                Exit Sub
            End If
        Next
        lblInput.text &= "."
    End Sub

    Protected Sub btnC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If lblInput.Type = TenKeyLabel.TenkeyType.TYP Then
            lblInput.Text = String.Empty
        Else
            lblInput.Text = "0"
        End If
    End Sub

    Protected Sub btnbar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If lblInput.Type = TenKeyLabel.TenkeyType.TYP Then
            lblInput.Text &= "-"
        ElseIf IsNumeric(lblInput.Text) Then
            If lblInput.Text.Equals("0") Then
                lblInput.Text = "-"
            Else
                If lblInput.Text.StartsWith("-") Then
                    lblInput.Text = lblInput.Text.Replace("-", "")
                    If String.IsNullOrEmpty(lblInput.Text) Then
                        lblInput.Text = "0"
                    End If
                Else
                    lblInput.Text = "-" & lblInput.Text
                End If
            End If
        Else
            If lblInput.Text.Equals("-") Then
                lblInput.Text = "0"
            Else
                lblInput.Text = "-"
            End If
        End If
    End Sub

    Protected Sub btnL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If lblInput.Type = TenKeyLabel.TenkeyType.TYP Then
            lblInput.Text &= "L"
        End If
    End Sub

    Protected Sub btnE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If lblInput.Type <> TenKeyLabel.TenkeyType.TYP Then
            If Not IsNumeric(lblInput.Text) Then
                lblInput.Text = "0"
            End If
        End If
        TenKeyDone()
    End Sub

    Overridable Sub TenKeyDone()
        lblInput.EndEdit()
        RaiseEvent InputCompleted(Me)
        Me.Dispose()
    End Sub

    Protected Function CheckNum() As Boolean
        If IsNumeric(lblInput.text) Then
            CheckNum = True
        Else
            CheckNum = False
        End If
    End Function
#End Region
End Class
